import { router } from "../../router";
const state = () => {
    return {
        tabsList: [{
            title: '首页',
            path: '/',
            activeMenu: '/',
            icon: 'el-icon-goods',
            closable: false,
        },],
        cacheViews: [],
        loading: false
    }
}

const mutations = {
    setCacheViews: (state, cacheViews) => {
        state.cacheViews = cacheViews
    },
    setLoading: (state, loading) => {
        state.loading = loading
    },
    openNewTab(state, { title, path, activeMenu, icon, closable }) {
        let has_path = false
        for (const index in state.tabsList) {
            const element = state.tabsList[index];
            if (element.path === path) {
                has_path = true
                break
            }
        }
        if (!has_path) {
            state.tabsList.push({ title, path, activeMenu, icon, closable })
        }

        router.push(path)
    },
    closeTab(state, path) {
        for (const index in state.tabsList) {
            const element = state.tabsList[index];
            if (element.path === path) {
                if (!element.closable) {
                    return
                }
                let length = state.tabsList.length
                let next_index = 0
                if (length <= 1) {
                    return
                }
                if (index > 0) {
                    next_index = index - 1
                } else {
                    next_index = index + 1
                }
                state.tabsList.splice(index, 1)
                router.push(state.tabsList[next_index].path)
                break
            };
        }
    }
}
const actions = {
    closeTab(context, { path }) {
        context.commit('closeTab', path)
    },
    openNewTab(context, { title, path, activeMenu, icon, closable }) {
        context.commit('openNewTab', { title, path, activeMenu, icon, closable })
    },
}
const getters = {

}


export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}